$(function() {
	/************************************根据name进行查找，内容补充，并且判断下级是否可用************************************************************************************************/
	$("#utm_source_name").bind({
	    keyup:function(){
	    	utm_source();
	    	if($.trim($("#utm_source_name").val())!=""&&$.trim($("#utm_source").val())!=""){
	    		$("#utm_medium_name").removeAttr("disabled");
	    		$("#utm_medium").removeAttr("disabled");
	    	}else{
	    		$("#utm_medium_name").attr("disabled",true);
	    		$("#utm_medium").attr("disabled",true);
	    	}
	    },
	    keypress:function(){
	    	utm_source();
	    	if($.trim($("#utm_source_name").val())!=""&&$.trim($("#utm_source").val())!=""){
	    		$("#utm_medium_name").removeAttr("disabled");
	    		$("#utm_medium").removeAttr("disabled");
	    	}else{
	    		$("#utm_medium_name").attr("disabled",true);
	    		$("#utm_medium").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_medium_name").bind({
	    keyup:function(){
	    	utm_General("utm_medium_name","utm_medium","utm_source");
	    	if($.trim($("#utm_medium_name").val())!=""&&$.trim($("#utm_medium").val())!=""){
	    		$("#utm_term_name").removeAttr("disabled");
	    		$("#utm_term").removeAttr("disabled");
	    	}else{
	    		$("#utm_term_name").attr("disabled",true);
	    		$("#utm_term").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	utm_General("utm_medium_name","utm_medium","utm_source");
	    	if($.trim($("#utm_medium_name").val())!=""&&$.trim($("#utm_medium").val())!=""){
	    		$("#utm_term_name").removeAttr("disabled");
	    		$("#utm_term").removeAttr("disabled");
	    	}else{
	    		$("#utm_term_name").attr("disabled",true);
	    		$("#utm_term").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_term_name").bind({
	    keyup:function(){
	    	utm_General("utm_term_name","utm_term","utm_medium");
	    	if($.trim($("#utm_term_name").val())!=""&&$.trim($("#utm_term").val())!=""){
	    		$("#utm_content_name").removeAttr("disabled");
	    		$("#utm_content").removeAttr("disabled");
	    	}else{
	    		$("#utm_content_name").attr("disabled",true);
	    		$("#utm_content").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	utm_General("utm_term_name","utm_term","utm_medium");
	    	if($.trim($("#utm_term_name").val())!=""&&$.trim($("#utm_term").val())!=""){
	    		$("#utm_content_name").removeAttr("disabled");
	    		$("#utm_content").removeAttr("disabled");
	    	}else{
	    		$("#utm_content_name").attr("disabled",true);
	    		$("#utm_content").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_content_name").bind({
	    keyup:function(){
	    	utm_General("utm_content_name","utm_content","utm_term");
	    	if($.trim($("#utm_content_name").val())!=""&&$.trim($("#utm_content").val())!=""){
	    		$("#utm_campaign_name").removeAttr("disabled");
	    		$("#utm_campaign").removeAttr("disabled");
	    	}else{
	    		$("#utm_campaign_name").attr("disabled",true);
	    		$("#utm_campaign").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	utm_General("utm_content_name","utm_content","utm_term");
	    	if($.trim($("#utm_content_name").val())!=""&&$.trim($("#utm_content").val())!=""){
	    		$("#utm_campaign_name").removeAttr("disabled");
	    		$("#utm_campaign").removeAttr("disabled");
	    	}else{
	    		$("#utm_campaign_name").attr("disabled",true);
	    		$("#utm_campaign").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_campaign_name").bind({
	    keyup:function(){
	    	utm_General("utm_campaign_name","utm_campaign","utm_content");
	    },
	    keydown:function(){
	    	utm_General("utm_campaign_name","utm_campaign","utm_content");
	    }
	});
	/**********************************************控制下级录入框是否可用**************************************************************************************/
	$("#utm_source").bind({
	    keyup:function(){
	    	if($.trim($("#utm_source_name").val())!=""&&$.trim($("#utm_source").val())!=""){
	    		$("#utm_medium_name").removeAttr("disabled");
	    		$("#utm_medium").removeAttr("disabled");
	    	}else{
	    		$("#utm_medium_name").attr("disabled",true);
	    		$("#utm_medium").attr("disabled",true);
	    	}
	    },
	    keypress:function(){
	    	if($.trim($("#utm_source_name").val())!=""&&$.trim($("#utm_source").val())!=""){
	    		$("#utm_medium_name").removeAttr("disabled");
	    		$("#utm_medium").removeAttr("disabled");
	    	}else{
	    		$("#utm_medium_name").attr("disabled",true);
	    		$("#utm_medium").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_medium").bind({
	    keyup:function(){
	    	if($.trim($("#utm_medium_name").val())!=""&&$.trim($("#utm_medium").val())!=""){
	    		$("#utm_term_name").removeAttr("disabled");
	    		$("#utm_term").removeAttr("disabled");
	    	}else{
	    		$("#utm_term_name").attr("disabled",true);
	    		$("#utm_term").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	if($.trim($("#utm_medium_name").val())!=""&&$.trim($("#utm_medium").val())!=""){
	    		$("#utm_term_name").removeAttr("disabled");
	    		$("#utm_term").removeAttr("disabled");
	    	}else{
	    		$("#utm_term_name").attr("disabled",true);
	    		$("#utm_term").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_term").bind({
	    keyup:function(){
	    	if($.trim($("#utm_term_name").val())!=""&&$.trim($("#utm_term").val())!=""){
	    		$("#utm_content_name").removeAttr("disabled");
	    		$("#utm_content").removeAttr("disabled");
	    	}else{
	    		$("#utm_content_name").attr("disabled",true);
	    		$("#utm_content").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	if($.trim($("#utm_term_name").val())!=""&&$.trim($("#utm_term").val())!=""){
	    		$("#utm_content_name").removeAttr("disabled");
	    		$("#utm_content").removeAttr("disabled");
	    	}else{
	    		$("#utm_content_name").attr("disabled",true);
	    		$("#utm_content").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_content").bind({
	    keyup:function(){
	    	if($.trim($("#utm_content_name").val())!=""&&$.trim($("#utm_content").val())!=""){
	    		$("#utm_campaign_name").removeAttr("disabled");
	    		$("#utm_campaign").removeAttr("disabled");
	    	}else{
	    		$("#utm_campaign_name").attr("disabled",true);
	    		$("#utm_campaign").attr("disabled",true);
	    	}
	    },
	    keydown:function(){
	    	utm_General("utm_content_name","utm_content","utm_term");
	    	if($.trim($("#utm_content_name").val())!=""&&$.trim($("#utm_content").val())!=""){
	    		$("#utm_campaign_name").removeAttr("disabled");
	    		$("#utm_campaign").removeAttr("disabled");
	    	}else{
	    		$("#utm_campaign_name").attr("disabled",true);
	    		$("#utm_campaign").attr("disabled",true);
	    	}
	    }
	});
	$("#utm_campaign").bind({
	    keyup:function(){
	    },
	    keydown:function(){
	    }
	});
});
/**
 * name
 * code
 * data
 */
function AutoCompleteKey(name,code,data){
	$('#'+name).autocomplete(data, {
		max: 12,    //列表里的条目数
		minChars: 0,    //自动完成激活之前填入的最小字符
		width: 170,     //提示的宽度，溢出隐藏
		scrollHeight: 300,   //提示的高度，溢出显示滚动条
		matchContains: true,    //包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示
		autoFill: false,    //自动填充
		formatItem: function(row, i, max) {
			return row.name;
		},
		formatMatch: function(row, i, max) {
			return row.name;
		},
		formatResult: function(row) {
			return row.name;
		}
	}).result(function(event, row, formatted) {
		$("#"+code).val(row.to);
	});
}
function utm_source(){
	$.ajax({
        type: "POST",
        url: "../userSource/getUserSource",
        data: { 
        	column_code	: "utm_source",
        	column_name : "utm_source_name",
	    	column_name_value : $("#utm_source_name").val(),
	    	up_column_code:"",
	    	up_column_code_value:""
        },
        dataType: "json",
        timeout: 2000,
        error: function (data) {
        },
        success: function (data) {
        	AutoCompleteKey("utm_source_name","utm_source",data);
        }
    });
}
function utm_General(column_name,column_code,up_column_code){
	if($.trim($("#"+up_column_code).val())==""){
		$.messager.alert('错误',"请完善上级信息！","info" ,function () {
			clear(column_name);
        });
	}else{
		$.ajax({
	        type: "POST",
	        url: "../userSource/getUserSource",
	        data: { 
	        	column_code	:column_code,
	        	column_name : column_name,
		    	column_name_value : $("#"+column_name).val(),
		    	up_column_code:up_column_code,
		    	up_column_code_value:$("#"+up_column_code).val()
	        },
	        dataType: "json",
	        timeout: 2000,
	        error: function (data) {
	        },
	        success: function (data) {
	        	AutoCompleteKey(column_name,column_code,data);
	        }
	    });
	}
	
	
}
/**
 * 根据那么进行清理
 * @param column_name
 */
function clear(column_name){
	$('#'+column_name).val("");
	$('#'+column_name).parent().parent().nextAll("input").val("");
}
